from pymongo import MongoClient

client = MongoClient(port=27017)
db=client['tiktok']
col='haventseen'
d={}
f={}
with open('C:\\Users\\Ziyue Luo\\Downloads\\as-feat-v1\\aesthetics_feature_'+col+'.txt','r',newline='\n',encoding='utf-8') as fileinput:
    for line in fileinput:
        lline=line.split('\t')
        id=lline[0].split('_')[0]
        img=lline[0].split('_')[1]
        af=[float(x) for x in lline[1].split(',')]
        if id in d.keys():
            f[id]+=1
            for i in range(len(d[id])):
                d[id][i]+=af[i]
        else:
            d[id]=af
            f[id]=1
for i in d.keys():
    d[i]=[x/f[i] for x in d[i]]
    db[col].update_one({'_id':i},{'$set':{'video_feature.editing.aesthetics_feature':d[i]}})